<?php

namespace app\admin\service;

use app\common\model\AdminMenuRole;
use app\common\model\AdminRole;
use app\common\model\AdminUser;
use app\common\model\AdminUserRole;
use app\common\model\Menu;

class AdminUserService
{
    protected $auth_role = [];

    public function login($param)
    {
        $adminUserModel = new AdminUser();
        $adminUser = $adminUserModel->where(['name' => $param['name']])->find();
        if (empty($adminUser['password'])) {
            return errorService('账号有误');
        }
        if ($adminUser) {
            if (md5(md5($param['password']) . $adminUser['salt']) != $adminUser['password']) {
                return errorService('密码有误');
            }
        }
        return successService($adminUser);
    }

    /**
     * @param $id  用户id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAdminUser($id)
    {
        $adminUserModel = new AdminUser();
        $adminUser = $adminUserModel->where(['id' => $id])->find();
        return successService($adminUser);
    }

    /**
     * @param $id  用户id'
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function selectAdminUserRole($id)
    {
        $adminUserRoleModel = new AdminUserRole();
        $adminUserRole = $adminUserRoleModel->where(['admin_user_id' => $id])->select();
        if (empty($adminUserRole)) {
            return errorService('该账号未绑定角色');
        }
        if ($adminUserRole){
            $adminUserRole = $adminUserRole->toArray();
        }
        $roleId = array_column($adminUserRole, 'role_id');
        $adminRoleModel = new AdminRole();
        if (in_array(1,$roleId)){
            $adminRole = $adminRoleModel->where('status', '=', 1)->select();
        }else{
            $adminRole = $adminRoleModel->where('id', 'in', $roleId)->where('status', '=', 1)->select();
        }
        if (empty($adminRole)) {
            return errorService('该账号未角色已禁言，请联系管理员');
        }
        if ($adminRole){
            $adminRole = $adminRole->toArray();
        }
        return successService($adminRole);
    }

    /**
     * @param $ids 角色id 列表
     * @param $url // 请求控制器
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getRoleMenu($ids, $url)
    {
        $menuModel = new Menu();
        if (in_array(1,$ids)){
            $menuList = $menuModel->order('pid','asc')->order('order','asc')->select();
        }else{
            $adminMenuRoleModel = new AdminMenuRole();
            $adminMenuList = $adminMenuRoleModel->where('admin_role_id','in',$ids)->select();
            if ($adminMenuList){
                $adminMenuList = $adminMenuList->toArray();
            }else{
                return errorService("该账号为配置菜单权限,请联系管理员");
            }
            $menuIds = array_column($adminMenuList,'admin_menu_id');
            $menuList = $menuModel->where('id','=',$menuIds)->order('order','asc')->order('pid','asc')->select()->toArray();

        }
        if (empty($menuList)){
            return errorService("该账号为配置菜单权限,请联系管理员");
        }else{
            $menuList = $menuList->toArray();
        }
    /*    if (!in_array(strtolower($url), $this->auth_role)) {
            $menuPath = array_column($menuList, 'path');
            if (!in_array(strtolower($url), array_map('strtolower', $menuPath))) {
                return errorService("该账号无权限,请联系管理员");
            }
        }*/
        return successService($menuList);
    }




}